// BSD License (http://www.galagosearch.org/license)

package org.galagosearch.core.parse;

import java.io.IOException;
import org.galagosearch.core.types.DocumentWordPosition;
import org.galagosearch.core.types.VersionedDocumentWordPosition;
import org.galagosearch.tupleflow.InputClass;
import org.galagosearch.tupleflow.OutputClass;
import org.galagosearch.tupleflow.StandardStep;
import org.galagosearch.tupleflow.Utility;
import org.galagosearch.tupleflow.execution.Verified;

/**
 *
 * @author trevor
 */
@Verified
@InputClass(className="org.galagosearch.core.parse.VersionedDocument")
@OutputClass(className="org.galagosearch.core.types.VersionedDocumentWordPosition")
public class VersionedPostingsPositionExtractor extends StandardStep<VersionedDocument, VersionedDocumentWordPosition> {
    public void process(VersionedDocument object) throws IOException {
    	int timeBegin=timeString2IntString(object.metadata.get("timeBegin"));
        int timeEnd=timeString2IntString(object.metadata.get("timeEnd"));
        int docID=Integer.parseInt(object.metadata.get("docID"));
        for(int i=0; i<object.terms.size(); i++) {
            String term = object.terms.get(i);
            if (term == null)
                continue;
            processor.process(new VersionedDocumentWordPosition(object.identifier, Utility.makeBytes(term), i,timeBegin,timeEnd,docID)); //these zeros are to be replaced with actual values of timebegin timeend and docid
        }
    }
    private int timeString2IntString(String a)
    {
    	//System.out.println("fromInsideOfMethod:\t"+a);
    	if(a.compareTo("-1")==0) return -1;
    	return Integer.parseInt(a.substring(2,4)+a.substring(5,7)+a.substring(8,10)+a.substring(11,13)+a.substring(14,16));
    
    }
}
